package com.yzh.croom.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yzh.croom.model.domain.*;
import com.yzh.croom.repository.RoleRepository;
import com.yzh.croom.service.AdminService;
import com.yzh.croom.service.MessageService;
import com.yzh.croom.service.RoomService;
import com.yzh.croom.service.VroomService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@Controller
@RequestMapping("/admin")
public class AdminController {
    @Autowired
    private MessageService messageService;
    @Autowired
    private RoomService roomService;
    @Autowired
    private VroomService vroomService;
    @Autowired
    private AdminService adminService;
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private RoleRepository roleRepository;

    @GetMapping("/toUnReadMessage")
    public String toMessage(Model model,
                            @RequestParam(defaultValue = "1") int pageNum) {
        PageHelper.startPage(pageNum, 5);
        List<Message> messages = messageService.getUnReadMessage();
        PageInfo<Message> pageInfo = new PageInfo<Message>(messages);
        model.addAttribute("pageInfo", pageInfo);
        model.addAttribute("tumessages", messages);
        return "admin/messgaeunread";
    }

    @PostMapping("/isApproval")
    @ResponseBody
    public int isApproval(int id) {
        int res = messageService.updateById(id);
        return res;
    }

    @GetMapping("/toIsReadMessage")
    public String toIsReadMessage(Model model,
                                  @RequestParam(defaultValue = "1") int pageNum) {
        PageHelper.startPage(pageNum, 5);
        List<Message> messages = messageService.getIsReadMessage();
        PageInfo<Message> pageInfo = new PageInfo<Message>(messages);
        model.addAttribute("pageInfo", pageInfo);
        model.addAttribute("tsmessages", messages);
        return "admin/messageisread";
    }

    @GetMapping("/toRoom")
    public String toRoom(Model model,
                         @RequestParam(defaultValue = "1") int pageNum) {
        PageHelper.startPage(pageNum, 6);
        List<Room> rs = roomService.getAllRoom();
        PageInfo<Room> pageInfo = new PageInfo<Room>(rs);
        model.addAttribute("toRoompageInfo", pageInfo);
        model.addAttribute("rooms", rs);
        return "admin/room";
    }

    @GetMapping("/toVroom")
    public String toVroom(Model model,
                          @RequestParam(defaultValue = "1") int pageNum) {
        PageHelper.startPage(pageNum, 5);
        List<VRoom> vRooms = vroomService.getVroom();
        PageInfo<VRoom> pageInfo = new PageInfo<VRoom>(vRooms);
        model.addAttribute("pageInfo", pageInfo);
        model.addAttribute("vrooms", vRooms);
        return "admin/vroom";
    }

    @GetMapping("/openRoomAdd")
    public String openRoomAdd() {
        return "admin/roomadd";
    }

    @PostMapping("/addRoom")
    @ResponseBody
    public int addRoom(@RequestBody Room room) {
        int res = roomService.addRoom(room);
        return res;
    }

    @GetMapping("/openRoomInfo")
    public String openRoomInfo(Model model, String roomId) {
        List<VRoom> rooms = vroomService.getVroomById(roomId);
        model.addAttribute("roomInfos", rooms);
        return "admin/roominfo";
    }

    @GetMapping("/openVroom")
    public String openVroom(Model model, String roomId) {
        model.addAttribute("roomId", roomId);
        return "admin/vroomadd";
    }

    @PostMapping("/addVroom")
    @ResponseBody
    public int addVroom(@RequestBody VRoom room) {
        room.setStatus(0);
        int res = vroomService.addVroom(room);
        return res;
    }

    @GetMapping("/getRoomById")
    public String getRoomById(Model model,
                              String roomId,
                              @RequestParam(defaultValue = "1") int pageNum) {
        PageHelper.startPage(pageNum, 5);
        List<VRoom> rooms = vroomService.getVroomById(roomId);
        PageInfo<VRoom> pageInfo = new PageInfo<VRoom>(rooms);
        model.addAttribute("pageInfo", pageInfo);
        model.addAttribute("svroom", rooms);
        return "admin/svroom";
    }


    @GetMapping("/toUploadFile")
    public String toUploadFile() {
        return "admin/uploadfile";
    }

    @PostMapping("/upload")
    public String upload(MultipartFile file, Model model) throws IllegalStateException, IOException {
        String path = "F:/upload/";
        File file1 = new File(path);
        if (!file1.exists()) {
            file1.mkdir();
        }
        String oldFileName = file.getOriginalFilename();
        File newFileName = new File(path + oldFileName);
        file.transferTo(newFileName);
        model.addAttribute("suc", "文件上传成功");
        return "admin/uploadfile";
    }

    @RequestMapping("/deleteMessagesByIds")
    @ResponseBody
    public int deleteMessagesByIds(String[] ids) {
        System.out.println(ids);
        int res = messageService.deleteMessageByIds(ids);
        return res;
    }

    @GetMapping("/toAdminadd")
    public String toAdminadd() {
        return "admin/adminadd";
    }

    @PostMapping("/adminadd")
    @ResponseBody
    public int adminAdd(@RequestBody User user) {
        BCryptPasswordEncoder cp = new BCryptPasswordEncoder();
        String password = cp.encode(user.getPassword());
        System.out.println(user.getPassword());
        user.setPassword(password);
        int res = adminService.addAdmin(user);
        return res;
    }

    @GetMapping("/toAdminlist")
    public String toAdminList(Model model,
                              @RequestParam(defaultValue = "1") int pageNum) {
        PageHelper.startPage(pageNum, 5);
        List<User> admins = adminService.getAllAdmin();
        PageInfo<User> pageInfo = new PageInfo<User>(admins);
        model.addAttribute("pageInfo", pageInfo);
        model.addAttribute("admins", admins);
        return "admin/adminlist";
    }

    @GetMapping("/updateStatus")
    @ResponseBody
    public int updateStatus(String account,int status) {
        int res = adminService.updateStatus(account, status);
        return res;
    }
}
